package com.e2oto.seller.modules.seller.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.e2oto.seller.R;
import com.e2oto.seller.common.base.BaseActivity;
import com.e2oto.seller.common.base.RecycleViewAdapter;
import com.e2oto.seller.common.utils.LogTools;
import com.e2oto.seller.common.utils.RxBus;
import com.e2oto.seller.modules.seller.adapter.SelectDistrictAdapter;
import com.e2oto.seller.modules.seller.bean.CityBean;
import com.e2oto.seller.modules.seller.bean.DistrictBean;
import com.e2oto.seller.modules.seller.bean.ProvinceBean;
import com.e2oto.seller.modules.seller.bean.RxBusBean;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ZhangLiQiang on 2017/11/14.
 */

public class SelectDistrictActivity extends BaseActivity {

    @BindView(R.id.select_address_rv)
    RecyclerView recyclerView;

    private SelectDistrictAdapter adapter;

    @Override
    protected void afterView(Bundle savedInstanceState) {

        final ProvinceBean provinceBean = (ProvinceBean) getIntent().getSerializableExtra("province");
        final CityBean cityBean = (CityBean) getIntent().getSerializableExtra("city");

        adapter = new SelectDistrictAdapter(JSONObject.parseArray(getIntent().getStringExtra("districts"), DistrictBean.class));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(R.id.item_address_name, new RecycleViewAdapter.ItemClickListener() {
            @Override
            public void onItemClicked(View view, int position) {

                adapter.resetStatus();
                adapter.getDataList().get(position).setSelected(true);
                adapter.notifyDataSetChanged();

                DistrictBean districtBean = adapter.getItem(position);
                RxBus.getIntanceBus().post(new RxBusBean<>(421, provinceBean.getName() + cityBean.getName() + districtBean.getName()));
                RxBus.getIntanceBus().post(new RxBusBean<>(422));//关闭选地址的所有页面
                finish();
            }
        });
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_select_address;
    }

    @OnClick(R.id.toolbar_left_btn)
    public void onClickBack() {
        finish();
    }
}
